package cn.com.server.controller.userManage;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import cn.com.server.common.AjaxCodeUtil;
import cn.com.server.common.AjaxObject;
import cn.com.server.entity.Group;
import cn.com.server.entity.Resource;
import cn.com.server.entity.Role;
import cn.com.server.service.GroupService;
import cn.com.server.service.ResourceService;
import cn.com.server.service.RoleService;
import cn.com.server.extend.view.GroupView;
import cn.com.server.extend.model.ResourceModel;
import cn.com.server.extend.view.ResourceView;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author qing
 * @since 2017-11-20
 */
@Controller
@RequestMapping("/userManage/resource")
public class ResourceController {
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private GroupService groupService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/resourceList")
	public String resourceList(HttpServletRequest request, HttpServletResponse response) {
		return "/userManager/resourceList";
	}
	
	@RequestMapping("/resourceTree")
	public String resourceTree(HttpServletRequest request, HttpServletResponse response) {
		return "/userManager/resourceTree";
	}
	
	@RequestMapping("/getResourceList")
	@ResponseBody
	public AjaxObject getResourceList(ResourceModel model,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		Page<Resource> pageData = resourceService.selectResourceList(model);
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(pageData);
		return ajaxObject;
	}
	
	@RequestMapping("/getGroupRoleTree")
	@ResponseBody
	public AjaxObject getGroupRoleTree(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		List<Group> tmp = groupService.selectByRegisterFlg(null);
		List<GroupView> pageData = JSON.parseArray(JSON.toJSONString(tmp),GroupView.class);
		for(GroupView gv:pageData) {
			List<Role> roles = roleService.selectByGroupId(gv.getId());
			gv.setChildren(roles);
		}
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(pageData);
		return ajaxObject;
	}
	
	@RequestMapping("/getResourceTree")
	@ResponseBody
	public AjaxObject getResourceTree(Long roleId,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		List<ResourceView> pageData = resourceService.selectResourceViewTreeIteration(0L, 1,roleId);
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(pageData);
		return ajaxObject;
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public AjaxObject getresourceTree(Resource model,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		if(model.getName()==null) {
			return AjaxCodeUtil.getAjaxObject("failed");
		}
		if(model.getSequence()==null) {
			return AjaxCodeUtil.getAjaxObject("failed");
		}
		resourceService.update(model);
		return AjaxCodeUtil.getAjaxObject("success");
	}
}
